package com.server.modules.api.web;

import com.server.common.persistence.Page;
import com.server.modules.api.Base.BaseAppController;
import com.server.modules.api.utils.Constants;
import com.server.modules.api.vo.BaseQueryVo;
import com.server.modules.api.vo.list.HomeImgList;
import com.server.modules.sys.entity.Dict;
import com.server.modules.sys.entity.about.About;
import com.server.modules.sys.entity.home.HomeImg;
import com.server.modules.sys.entity.lanchimage.LanchImage;
import com.server.modules.sys.entity.loan.Loan;
import com.server.modules.sys.entity.member.Member;
import com.server.modules.sys.service.DictService;
import com.server.modules.sys.service.about.AboutService;
import com.server.modules.sys.service.lanchimage.LanchImageService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 首页轮播图接口
 * Created by Administrator on 2016/7/21.
 */
@Controller
@RequestMapping(value = "/api/home")
public class HomeImageListAPIController extends BaseAppController {
	@Resource
	private LanchImageService lanchImageService;
	@Resource
	private AboutService aboutService;

	@Resource
	private DictService dictService;
	@ResponseBody
	@RequestMapping(value = "/getImageList", method = RequestMethod.POST)
	public BaseQueryVo getImageList(HttpServletRequest request, String sessionId) {
	//	ImageListVo<HomeImgList> vo = new ImageListVo<HomeImgList>();
		BaseQueryVo vo = new BaseQueryVo();
		HomeImg homeImg = new HomeImg();
		Page<HomeImg> page = new Page<HomeImg>();
		page.setOrderBy("orderids asc");//按orderids字段升序排序
		homeImg.setPage(page);
		//获取轮播图列表
		List<HomeImg> homeImgs= homeImgService.findList(homeImg);
		//如果获取失败
		if (homeImgs == null || homeImgs.size() == 0) {
			vo.setRetMsg("无轮播图信息");
			vo.setRetCode(120);
		} else {
			//设置轮播图所关联的贷款名称 objId不一定指贷款  所以不再sql里面查
			for (HomeImg homeImg2 : homeImgs) {
				String objId = homeImg2.getObjId();
				if (objId ==null){
					continue;
				}
				Loan loan = loanService.get(objId);
				if (loan !=null){
					if (loan.getName()!=null)
					homeImg2.setObjName(loan.getName());
				}
			}
			vo.setRetCode(0);
			vo.setRetMsg("轮播图获取成功");
			HomeImgList homeImgList = new HomeImgList(homeImgs);
			vo.setData(homeImgList);
		}
		return vo;
	}
	@ResponseBody
	@RequestMapping(value = "/getLanchImage", method = RequestMethod.POST)
	public BaseQueryVo getLanchImage(HttpServletRequest request) {
		BaseQueryVo baseQueryVo = new BaseQueryVo();
		LanchImage lanchImage = lanchImageService.findOneLanchImage();
		if (lanchImage != null){
			if (lanchImage.getUrl() != null && lanchImage.getUrl().length() > 0) {
				lanchImage.setUrl(lanchImage.getUrl());
			    baseQueryVo.setData(lanchImage);
			}
		}
		return baseQueryVo;
	}
	@ResponseBody
	@RequestMapping(value = "/about", method = RequestMethod.GET)
	public ModelAndView about(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/modules/api/about/about");
		return mv;
	}
	@ResponseBody
	@RequestMapping(value = "/readme", method = RequestMethod.GET)
	public ModelAndView readme(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/modules/api/readme/readme");
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = "/checkVersion", method = RequestMethod.POST)
	public BaseQueryVo checkVersion(HttpServletRequest request,About about) {
		BaseQueryVo baseQueryVo = new BaseQueryVo();
		if (StringUtils.isEmpty(about.getOstype())){
			return returnMsgAPIService.getRet(Constants.incompleteData);
		}
		if (StringUtils.isEmpty(about.getVersion())){
			return returnMsgAPIService.getRet(Constants.incompleteData);
		}

		About lastAbout =  aboutService.getLastAbout(about);
		baseQueryVo.setRetCode(0);
		baseQueryVo.setRetMsg("成功");
		if (lastAbout != null){
			baseQueryVo.setData(lastAbout);
		}
		return baseQueryVo;
	}

	@ResponseBody
	@RequestMapping(value = "/initDict", method = RequestMethod.POST)
	public BaseQueryVo initDict(HttpServletRequest request) {
		BaseQueryVo baseQueryVo = new BaseQueryVo();
		List<Dict> list = dictService.findList(new Dict());
		if (list != null && list.size() > 0){
			baseQueryVo.setData(list);
		}
		return baseQueryVo;
	}

	@ResponseBody
	@RequestMapping(value = "/download", method = RequestMethod.GET)
	public ModelAndView download(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
        List<About> list = aboutService.findList(new About());
        if (list !=null && list.size() > 0){
			About about = list.get(0);
			String ostype = about.getOstype();
			if("iOS".equals(ostype)){
				request.setAttribute("iosUrl",about.getUrl());
			}else if("Android".equals(ostype)){
				request.setAttribute("androidUrl",about.getUrl());
			}
		}
        mv.setViewName("/modules/api/download/download");
		return mv;
	}
	@ResponseBody
	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public BaseQueryVo test(HttpServletRequest request,BaseQueryVo vo){
		Member member = memberService.get(vo.getMemberId());
		if (member != null){
			List<String> userList = new ArrayList<String>(1);
			userList.add(member.getHxUsername());
			Map<String,String > msg = new HashedMap();
			msg.put("type","text");
			msg.put("msg","订单号为："+"的申请已支付成功，请耐心等待客服人员与您进行联系");
			Map<String,String > ext = new HashedMap();
			ext.put("order","222");
			imService.sendMessages("users",userList,msg,"客服经理",ext);
			vo.setRetCode(0);
			vo.setRetMsg("完成");
		}else {
			vo.setRetCode(-1);
			vo.setRetMsg("失败");
		}
		return vo;
	}




}
